import { Button, Popconfirm, Tag, Switch } from "antd";
import Blcon from "../../components/Blcon";
import moment from "moment";
// 文章column
export const articleColumn = ({
  setVisible,
  handleDialogTypeAndId,
  deleteById,
  handleChangeIsTop
}) => {
  const columns = [
    {
      align: "center",
      title: "文章封面",
      width: 200,
      dataIndex: "titleImg",
      render: (titleImg) => {
        return (
          <img
            src={titleImg || ""}
            style={{ width: "100%", height: "100%" }}
          ></img>
        );
      },
    },
    {
      width: 200,
      align: "center",
      title: "文章名称",
      dataIndex: "title",
      key: "title",
    },
    {
      width: 100,
      align: "center",
      title: "文章作者",
      dataIndex: "authorInfo",
      key: "authorInfo",
      render: (row) => {
        return row.nickname;
      },
    },
    {
      width: 100,
      align: "center",
      title: "类型",
      dataIndex: "composeType",
      render: (type) => {
        if (type===1) {
          return <Tag color="green">原创</Tag>
        } else if(type===0) {
          return <Tag color="green">转载</Tag>
        }else {
          return "--";
        }
      },
    },
    {
      width: 200,
      align: "center",
      title: "标签",
      dataIndex: "tagList",
      key: "tagList",
      render: (row) => {
        return row && row.length > 0 ? (
          <div style={{ display: "flex", justifyContent: "center" }}>
            {row.map((item) => (
              <Tag color="skyblue" key={item.id}>
                {item.tagName}
              </Tag>
            ))}
          </div>
        ) : (
          "--"
        );
      },
    },
    {
      width: 100,
      align: "center",
      title: "分类",
      dataIndex: "typeInfo",
      render: (info) => {
        return info?<Tag color="gold">{info.typeName}</Tag>:"--"
      },
    },
    {
      width: 150,
      align: "center",
      title: "置顶",
      // dataIndex: "isTop",
      render: (row) => {
        return <Switch checked={row.isTop === 1 ? true : false} onChange={(checked,e)=>handleChangeIsTop(row.id,checked)}></Switch>;
      },
    },
    {
      width: 100,
      align: "center",
      title: "状态",
      dataIndex: "status",
      render: (status) => {
        let color = "";
        let text = "";
        switch (status) {
          case -1:
            color = "red";
            text = "下架";
            break;
          case 0:
            text = "待审核";
            color = "cyan";
            break;
          case 1:
            text = "发布";
            color = "purple";
            break;
          case 2:
            text = "草稿";
            color = "#f50";
            break;
        }
        return text ? <Tag color={color}>{text}</Tag> : "--";
      },
    },
    {
      width: 200,
      align: "center",
      title: "添加时间",
      dataIndex: "createTime",
      render: (row) => {
        return row ? moment(parseInt(row)).format("YYYY-MM-DD HH:mm:ss") : "--";
      },
    },

    {
      align: "center",
      title: "操作",
      width: 200,
      fixed: "right",
      render: (row) => {
        return (
          <div>
            <Button
              style={{
                background: "greenyellow",
                color: "#fff",
                borderColor: "greenyellow",
                marginLeft: "10px",
              }}
              size="small"
            >
              发布
            </Button>
            <Button
              type="primary"
              style={{ marginLeft: "10px" }}
              onClick={() => {
                setVisible(true);
                handleDialogTypeAndId(row.id);
              }}
              size="small"
            >
              编辑
            </Button>
            <Popconfirm
              title="提示"
              description="确定要删除吗?"
              onConfirm={() => deleteById(row.id)}
              okText="确认"
              cancelText="取消"
            >
              <Button
                danger
                type="primary"
                style={{ marginLeft: "10px" }}
                size="small"
              >
                删除
              </Button>
            </Popconfirm>
          </div>
        );
      },
    },
  ];

  return columns;
};

// 标签column
export const TagColumn = ({ changeVisible, deleteConfirm }) => {
  const column = [
    {
      align: "center",
      title: "标签名",
      dataIndex: "tagName",
    },
    {
      align: "center",
      title: "文章量",
      dataIndex: "count",
    },
    {
      align: "center",
      title: "点击量",
      dataIndex: "clickNum",
    },
    {
      align: "center",
      title: "创建时间",
      render: (row) => {
        if (!row.createTime) {
          return "--";
        } else {
          return moment(parseInt(row.createTime)).format("YYYY-MM-DD HH:mm:ss");
        }
      },
    },
    {
      align: "center",
      title: "操作",
      render: (row) => {
        return (
          <div>
            <Button
              type="primary"
              style={{ marginLeft: "10px" }}
              size="small"
              onClick={() => changeVisible(1, row.id)}
            >
              编辑
            </Button>

            <Popconfirm
              title="提示"
              description="确定要删除吗?"
              onConfirm={() => deleteConfirm(row.id)}
              okText="确认"
              cancelText="取消"
            >
              <Button
                danger
                type="primary"
                style={{ marginLeft: "10px" }}
                size="small"
              >
                删除
              </Button>
            </Popconfirm>
          </div>
        );
      },
    },
  ];
  return column;
};

//分类column
export const cateColumn = ({ changeVisible, deleteConfirm }) => {
  const columns = [
    {
      align: "center",
      title: "图标",
      dataIndex: "icon",
      render: (row) => {
        return <Blcon type={row}></Blcon>;
      },
    },
    {
      align: "center",
      title: "分类名",
      dataIndex: "typeName",
    },
    {
      align: "center",
      title: "文章量",
      dataIndex: "count",
    },
    {
      align: "center",
      title: "点击量",
      dataIndex: "clickNum",
    },
    {
      align: "center",
      title: "添加时间",
      render: (row) => {
        if (!row.createTime) {
          return "--";
        } else {
          return moment(parseInt(row.createTime)).format("YYYY-MM-DD HH:mm:ss");
        }
      },
    },
    {
      align: "center",
      title: "操作",
      render: (row) => {
        return (
          <div>
            <Button
              size="small"
              style={{
                background: "greenyellow",
                color: "#fff",
                borderColor: "greenyellow",
                marginLeft: "10px",
              }}
            >
              发布
            </Button>
            <Button
              size="small"
              type="primary"
              style={{ marginLeft: "10px" }}
              onClick={() => changeVisible(1, row.id)}
            >
              编辑
            </Button>
            <Popconfirm
              title="提示"
              description="确定要删除吗?"
              onConfirm={() => deleteConfirm(row.id)}
              okText="确认"
              cancelText="取消"
            >
              <Button
                danger
                size="small"
                type="primary"
                style={{ marginLeft: "10px" }}
              >
                删除
              </Button>
            </Popconfirm>
          </div>
        );
      },
    },
  ];

  return columns;
};
